window['console']['log'] = function () {};
$(function () {
    $("button").attr("type", "button");
    var url = document.location.toString(); //获取url地址
    // console.log(url);
    var urlParmStr = url.slice(url.indexOf('?') + 1); //获取问号后所有的字符串
    // console.log(urlParmStr);
    var arr = urlParmStr.split('&'); //通过&符号将字符串分割转成数组
    // console.log(arr);
    var id = arr[0].split("=")[1]; //第一个参数
    // console.log(id);
    var title = arr[1].split("=")[1]; //第二个参数
    title = decodeURI(title); //转码将解码方式unscape换为decodeURI，将中文参数获取
    $('.pageRightContentOne span').append(title)
    // 获取课时列表
    var data = {
        t: "1559010885",
        r: "z2eG2RFma",
        z: "0E7E1911542C66EAA8081132D1CFDF5E",
        courseId: id
    }
    $.ajax({
        //url: url + "admin/getCourseLesson",
        url: "http://www.zhenggod.com/admin/getCourseLesson",
        type: "post",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded",
        data: data,
        success: function (res) {
            var arr = []
            var data = res.data;
            for (var i = 0; i < data.length; i++) {

                // 日期转时间戳
                function timestampToTime() {
                    let time = data[i].updatedTime;
                    var date = new Date(time * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
                    var Y = date.getFullYear() + '-';
                    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                    var D = date.getDate() + ' ';
                    return Y + M + D;
                }
                timestampToTime();
                // console.log(data[i].id);
                var item =
                    ` 
                            <tr>
                                <td><input type="hidden"></td>
                                <td>${data[i].id}</td>
                                <td>${data[i].title}</td>
                                <td>${data[i].length}</td>
                                <td>${timestampToTime()}</td>
                                <td class="courseOperation"><a class="addClassHourBtn">编辑</a><a class="classroomOpen">删除</a></td>
                                <td><input type="hidden" value="${data[i].mediaUri}"></td>
                            </tr>
                        `
                arr.push(item)
            }
            $('.getClassHour tbody').html(arr)
        }
    });
    // 添加课时
    $(document).on('click', '.addClassHourBtn', function () {
        let val = $(this).text()
        console.log(val);
        if (val == '编辑') {
            $('#addBtn').hide();
            $('#saveBtn').show();
            var id = $(this).parent().parent().children().eq(1).text();
            var title = $(this).parent().parent().children().eq(2).text();
            var length = $(this).parent().parent().children().eq(3).text();
            var data = $(this).parent().parent().children().eq(4).text();
            var mediaUri = $(this).parent().next().children().eq(0).val();
            $('#classHourId').val(id);
            $('#className').val(title);
            $('#classPrice').val(mediaUri);
            $('#testView').text(data);
            $('#classHouerTime').val(length);
            // $('#className').val(name);
        } else if (val == '添加') {
            $('#addBtn').show();
            $('#saveBtn').hide();
        }
        $('.getClassHour').hide();
        $('.addClassHour').show();

    })
    $('#saveBtn').click(function () {
        var id = $('#classHourId').val();
        var name = $('#className').val();
        var mediaUri = $('#classPrice').val();
        var length = $('#classHouerTime').val();
        var time = $('#testView').text();
        var date = new Date(time);
        //  时间戳
        var time1 = date.getTime();
        var newTime = time1 / 1000;
        var data = {
            t: "1559010885",
            r: "z2eG2RFma",
            z: "0E7E1911542C66EAA8081132D1CFDF5E",
            lessonId: id,
            lessonName: name,
            mediaUri: mediaUri,
            length: length,
            startTime: newTime
        }
        $.ajax({
            //url: url + "admin/getCourseLesson",
            url: "http://www.zhenggod.com/admin/updateCourseLesson",
            type: "post",
            dataType: "json",
            contentType: "application/x-www-form-urlencoded",
            data: data,
            success: function (res) {
                console.log(res);
            }

        })
    })
    // 获取下拉
    $.ajax({
        url: url + "admin/getClassroomCategoryList",
        type: "post",
        dataType: "json",
        contentType: "application/x-www-form-urlencoded",
        data: data,
        success: function (res) {
            var arr = [];
            for (var i = 0; i < res.data.length; i++) {
                var item = `
          <option value="${res.data[i].id}">${res.data[i].name}</option>
          `;
                arr.push(item);
            }
            $("#courseDown").append(arr);
            //   $('#addClassroomDropDown').append(arr)
            renderSelect();
        }
    });
    // $('#addClassHourBtn').click(function () {

    // })
    $(".backBtn").click(function () {
        $('.getClassHour').show();
        $('.addClassHour').hide();
        document.getElementById("addform").reset();
    })
    // 日历点击开关
    $('.chooseTime').click(function () {
        $('.timeTable').toggle();
    });
    $('.laydate-btns-confirm').click(function () {
        $('.timeTable').hide()
    })
    $('#addBtn').click(function () {
        var name = $('#className').val();
        var mediaUri = $('#classPrice').val();
        var time = $('#testView').text();
        var lenght = $('#classHouerTime').val();
        console.log(lenght);
        // console.log($('#testView').text());
        var date = new Date(time);
        //  时间戳
        var time1 = date.getTime();
        var newTime = time1 / 1000;
        // console.log(newTime);
        data = {
            t: "1559010885",
            r: "z2eG2RFma",
            z: "0E7E1911542C66EAA8081132D1CFDF5E",
            courseId: id,
            title: name,
            mediaUri: mediaUri,
            length: lenght,
            startTime: newTime,
        }
        $.ajax({
            url: "http://www.zhenggod.com/admin/addCourseLesson",
            type: "post",
            dataType: "json",
            contentType: "application/x-www-form-urlencoded",
            data: data,
            success: function (res) {
                console.log(res);
                $('.AddSuccess').show();
                $('.addClassHour').hide();
            }
        });
    })
    $('.returnClassBtn').click(function () {
        $('.AddSuccess').hide();
        $('.getClassHour').show();
        document.getElementById('addform').reset();
    });
    $('.addClassBtn').click(function () {
        $('.AddSuccess').hide();
        $('.addClassHour').show();
        document.getElementById('addform').reset();
    })

})
// 日历组件
layui.use('laydate', function () {
    var laydate = layui.laydate;
    laydate.render({
        elem: '#test2',
        position: 'static',
        change: function (value, date) { //监听日期被切换
            lay('#testView').html(value);
            // console.log(value)
        }
    })
});